package com.caizongzhen.www.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.caizongzhen.www.entity.Medicine;
import com.caizongzhen.www.entity.User;
import com.caizongzhen.www.util.VerifyFormat;
import org.springframework.ui.Model;
import com.caizongzhen.www.dto.VerifyParaDto;
import com.caizongzhen.www.entity.FeedBack;
import com.caizongzhen.www.service.FeedBackService;
import com.caizongzhen.www.util.Assert;
import com.caizongzhen.www.util.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class FeedBackController {
    @Autowired
    private FeedBackService feedBackService;

    /*
     * 反馈页面
     * */
    @RequestMapping("/feedback")
    public String FeedBack(Model model){
        return  "feedback";
    }


    /*
    * 反馈提交接口
    * */
    @RequestMapping("/feedback/save")
    @ResponseBody
    public RespResult saveFeedBack(FeedBack feedback){
        VerifyParaDto verifyParaDto = new VerifyParaDto();
        verifyParaDto = judgeSaveFeedBack(feedback);
        if (!verifyParaDto.Tag) {
            return RespResult.fail(verifyParaDto.Msg);
        }
        feedback.setCreateTime(new Date());
        feedback.setUpdateTime(new Date());
        boolean bool = feedBackService.save(feedback);
        if (bool) return RespResult.success("发布成功");
        else return RespResult.fail("发布失败");
    }

    /*
    *  所有反馈信息页面
    * */
    @RequestMapping("all-feedback")
    public  String allFeedBack(Model model, @RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize, HttpSession session){
        // 判断是否登录
        User user =(User) session.getAttribute("loginUser");
        if (Assert.isEmpty(user)) return "redirect:/index";
        IPage<FeedBack> feedBackList = feedBackService.getFeedBackPageDate(currentPage,pageSize);
        model.addAttribute("feedbackList",feedBackList.getRecords());
        return "all-feedback";
    }

    /*
     * 删除反馈信息
     * */
    @PostMapping("/feedback/delete")
    @ResponseBody
    public RespResult feedBackDelete(Integer id, HttpSession session){
        if (Assert.isEmpty(id)) return RespResult.fail("id不能为空");
        FeedBack feedBack = feedBackService.getById(id);
        if (Assert.isEmpty(feedBack)) return RespResult.fail("传入的id有误");
        boolean bool = feedBackService.removeById(id);
        if (bool) return RespResult.success("删除成功");
        else return RespResult.fail("删除失败");
    }


    /*
    *  验证提交反馈的参数是否合法
    * */
    private VerifyParaDto judgeSaveFeedBack(FeedBack feedback) {
        VerifyParaDto verifyParaDto = new VerifyParaDto();
        verifyParaDto.Tag = false;
        if (Assert.isEmpty(feedback.getName())) verifyParaDto.Msg = "姓名不能为空";
        else if (Assert.isEmpty(feedback.getEmail())) verifyParaDto.Msg = "邮箱不能为空";
        else if (Assert.isEmpty(feedback.getTitle())) verifyParaDto.Msg = "反馈标题不能为空";
        else if (Assert.isEmpty(feedback.getContent())) verifyParaDto.Msg = "反馈内容不能为空";
        else if (!VerifyFormat.verifyFormatEmail(feedback.getEmail())) {
            verifyParaDto.Msg = "邮箱格式不正确";
        } else {
            verifyParaDto.Tag = true;
        }
        return  verifyParaDto;

    }
}
